﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CoruptXMLFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = args[0];

            FileStream fs = Win32File.Open(filePath, FileMode.Open, FileAccess.ReadWrite);
            StreamReader reader = new StreamReader(fs);
            string xmlText = reader.ReadToEnd();

            char[] buffer = new char[xmlText.Length + 2];
            int index=0;
            foreach(char c in xmlText)
            {
                buffer[index] = c;
                index++;
            }
            
            buffer[xmlText.Length] = (char)0x00;
            buffer[xmlText.Length + 1] = (char)0x02;

            fs.SetLength(0);
            fs.Flush();

            StreamWriter writer = new StreamWriter(fs, System.Text.Encoding.UTF8);
            writer.Write(new String(buffer,0,xmlText.Length + 2));
            fs.Flush();
            writer.Close();
            fs.Close();

            Console.WriteLine("Finished!");
            Console.ReadLine();
        }
    }
}
